<?php
namespace MeetingManage\Controller;

use Think\Controller;

class ConferenceController extends Controller
{
    //查看具体内容
    public function ShowDetail()
    {
        $Conference = D('Conference');
        $where['id'] = I('get.id');
        $result = $Conference->where($where)->find();
        $this->ajaxReturn($result);
    }

    public function detail()
    {
        $Conference = D('Conference');
        $where['id'] = I('get.id');
        $result = $Conference->where($where)->find();
        $this->assign('result',$result);
        $this->display();
    }
    
    //创建会议记录
    public function create_con()
    {
        $this->display();
    }

    public function CreateConfer()
    {
        $Conference = D('Conference');//实例化对象
            if ($Conference->create()) {
                $Conference->author = session('id');
                $Conference->add();
                $data['status'] = 1;//创建成功
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 0;//创建失败
                $data['info'] = $Conference->getError();//获得错误信息
                $this->ajaxReturn($data);//ajaxreturn，返回错误信息和状态
            }
    }

    //会议记录修改
    public function update_con($id)
    {
        $Conference = D('Conference');
        $data = $Conference->field('id,author')->where("id=$id")->find();
        if ((session('status') == 4) || (session('id') == $data['author'])) {
            $this->assign('data', $data);
            $this->display();
        } else {
            $this->error('权限不足', 'show_con.html');
        }

    }

    public function Update($id)
    {
        $Conference = D('Conference');
        if (!empty($_POST)) {
            $data = $Conference->create();
            if ($data) {
                $Conference->id = $id;
                $Conference->filename = I('filename');
                $Conference->filesave = I('filesave');
                $result = $Conference->save();
                if ($result) {
                    $status = 1;//修改成功
                } else {
                    $status = 0;//修改失败
                }
            } else {
                $status = 2;//数据为空
            }
        }
        $this->ajaxReturn($status);
    }

    //删除会议记录
    public function delete_con()
    {
        if (!empty($_POST)) {
            if (session('status') == 4) {
                $where['id'] = I('post.id');
                $data = M('Conference');
                $result = $data->where($where)->delete();
                if ($result) {
                    $status=1;
                    $this->ajaxReturn($status);
                } else {
                    $status=0;
                    $this->ajaxReturn($status);
                }
            }
        } else {
            $data = D('Conference');
            $where['id'] = I('post.id');
            $result = $data->field('author')->where($where)->find();
            if (session('id') == $result) {
                $where['id'] = I('post.id');
                $data = M('Conference');
                $result = $data->where($where)->delete();
                if ($result) {
                    $status=1;//删除成功
                    $this->ajaxReturn($status);
                } else {
                    $status=0;//删除失败
                    $this->ajaxReturn($status);
                }
            } else {
                $status=2;//没有权限
                $this->ajaxReturn($status);
            }
        }

    }
    
    //展示所有的会议记录
    public function ShowPage()
    {
        $data = D('Conference');
        $result = $data
            ->field('id,title,sort,date')
            ->order('id desc')
            ->select();
        $this->ajaxReturn($result);
    }

    public function show_con()
    {
        $this->display();
    }

    //文件上传
    public function upload()
    {
        $upload = new \Think\Upload();
        $upload->rootPath = './Public/Meeting/';
        $upload->savePath = '';
        $upload->autoSub = false;

        $info = $upload->upload($_FILES);

        foreach ($info as $file) {
            $startname = $file['name'];
            $savename = $file['savename'];
        }
        $data[0]=$startname;
        $data[1]=$savename;
        $this->ajaxReturn($data);
    }

    public function getname()
    {
        $user=M('User');
        $result=$user->where('status>0')->field('name')->select();
        $this->ajaxReturn($result);
    }
}